package com.inei.cie.dao;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.inei.cie.R;
import com.inei.cie.entidades.P5;
import com.inei.cie.entidades.P5_F;
import com.inei.cie.entidades.P5_N;
import com.inei.cie.utilitarios.Sesion;

public class UtilDB extends SQLiteOpenHelper {

	protected static Context context;
	public static String DATABASE = "cie.db";
	public static int VERSION = 10;

	public UtilDB(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public UtilDB(Context ctx) {
		super(ctx, DATABASE, null, VERSION);
		context = ctx;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql;
		InputStream in;
		Document doc;
		NodeList statements;
		int i;

		try {
			DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			// Car
			in = context.getResources().openRawResource(R.raw.car);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Car");
			Log.i("inei", "Car");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// Dpto
			in = context.getResources().openRawResource(R.raw.dpto);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Dpto");
			Log.i("inei", "Dpto");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// Prov
			in = context.getResources().openRawResource(R.raw.prov);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Prov");
			Log.i("inei", "Prov");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// Dist
			in = context.getResources().openRawResource(R.raw.dist);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Dist");
			Log.i("inei", "Dist");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// PCar
			in = context.getResources().openRawResource(R.raw.sqlpcar);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("PCar");
			Log.d("inei", statements.getLength() + "");
			Log.i("inei", "PCar");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// PCar_C_1N
			in = context.getResources().openRawResource(R.raw.sqlpcar_c_1n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("PCar_C_1N");
			Log.i("inei", "PCar_C_1N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_A
			in = context.getResources().openRawResource(R.raw.sqlp1_a);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_A");
			Log.i("inei", "P1_A");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_A_2N
			in = context.getResources().openRawResource(R.raw.sqlp1_a_2n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_A_2N");
			Log.i("inei", "P1_A_2N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_A_2_8N
			in = context.getResources().openRawResource(R.raw.sqlp1_a_2_8n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_A_2_8N");
			Log.i("inei", "P1_A_2_8N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_A_2_9N
			in = context.getResources().openRawResource(R.raw.sqlp1_a_2_9n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_A_2_9N");
			Log.i("inei", "P1_A_2_9N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_B
			in = context.getResources().openRawResource(R.raw.sqlp1_b);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_B");
			Log.i("inei", "P1_B");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_B_3N
			in = context.getResources().openRawResource(R.raw.sqlp1_b_3n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_B_3N");
			Log.i("inei", "P1_B_3N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_B_3_12N
			in = context.getResources().openRawResource(R.raw.sqlp1_b_3_12n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_B_3_12N");
			Log.i("inei", "P1_B_3_12N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_B_2A_N
			in = context.getResources().openRawResource(R.raw.sqlp1_b_2a_n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_B_2A_N");
			Log.i("inei", "P1_B_2A_N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_C
			in = context.getResources().openRawResource(R.raw.sqlp1_c);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_C");
			Log.i("inei", "P1_C");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P1_C_20N
			in = context.getResources().openRawResource(R.raw.sqlp1_c_20n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P1_C_20N");
			Log.i("inei", "P1_C_20N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_A
			in = context.getResources().openRawResource(R.raw.sqlp2_a);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_A");
			Log.i("inei", "P2_A");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_B
			in = context.getResources().openRawResource(R.raw.sqlp2_b);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_B");
			Log.i("inei", "P2_B");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_B_9N
			in = context.getResources().openRawResource(R.raw.sqlp2_b_9n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_B_9N");
			Log.i("inei", "P2_B_9N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_B_12N
			in = context.getResources().openRawResource(R.raw.sqlp2_b_12n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_B_12N");
			Log.i("inei", "P2_B_12N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_B_11N
			in = context.getResources().openRawResource(R.raw.sqlp2_b_11n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_B_11N");
			Log.i("inei", "P2_B_11N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_B_10N
			in = context.getResources().openRawResource(R.raw.sqlp2_b_10n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_B_10N");
			Log.i("inei", "P2_B_10N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_C
			in = context.getResources().openRawResource(R.raw.sqlp2_c);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_C");
			Log.i("inei", "P2_C");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D
			in = context.getResources().openRawResource(R.raw.sqlp2_d);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D");
			Log.i("inei", "P2_D");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D_1N
			in = context.getResources().openRawResource(R.raw.sqlp2_d_1n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D_1N");
			Log.i("inei", "P2_D_1N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D_3N
			in = context.getResources().openRawResource(R.raw.sqlp2_d_3n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D_3N");
			Log.i("inei", "P2_D_3N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D_5N
			in = context.getResources().openRawResource(R.raw.sqlp2_d_5n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D_5N");
			Log.i("inei", "P2_D_5N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D_7N
			in = context.getResources().openRawResource(R.raw.sqlp2_d_7n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D_7N");
			Log.i("inei", "P2_D_7N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_D_9N
			in = context.getResources().openRawResource(R.raw.sqlp2_d_9n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_D_9N");
			Log.i("inei", "P2_D_9N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_E
			in = context.getResources().openRawResource(R.raw.sqlp2_e);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_E");
			Log.i("inei", "P2_E");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_F
			in = context.getResources().openRawResource(R.raw.sqlp2_f);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_F");
			Log.i("inei", "P2_F");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_G
			in = context.getResources().openRawResource(R.raw.sqlp2_g);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_G");
			Log.i("inei", "P2_G");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P2_G_2N
			in = context.getResources().openRawResource(R.raw.sqlp2_g_2n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P2_G_2N");
			Log.i("inei", "P2_G_2N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P3
			in = context.getResources().openRawResource(R.raw.sqlp3);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P3");
			Log.i("inei", "P3");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P3_GPS
			in = context.getResources().openRawResource(R.raw.sqlp3_gps);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P3_GPS");
			Log.i("inei", "P3_GPS");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P4
			in = context.getResources().openRawResource(R.raw.sqlp4);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P4");
			Log.i("inei", "P4");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P4_2N
			in = context.getResources().openRawResource(R.raw.sqlp4_2n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P4_2N");
			Log.i("inei", "P4_2N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P5
			in = context.getResources().openRawResource(R.raw.sqlp5);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P5");
			Log.i("inei", "P5");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P5_N
			in = context.getResources().openRawResource(R.raw.sqlp5_n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P5_N");
			Log.i("inei", "P5_N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P5_F
			in = context.getResources().openRawResource(R.raw.sqlp5_f);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P5_F");
			Log.i("inei", "P5_F");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P6_1
			in = context.getResources().openRawResource(R.raw.sqlp6_1);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P6_1");
			Log.i("inei", "P6_1");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P6_1_8N
			in = context.getResources().openRawResource(R.raw.sqlp6_1_8n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P6_1_8N");
			Log.i("inei", "P6_1_8N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P6_1_10N
			in = context.getResources().openRawResource(R.raw.sqlp6_1_10n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P6_1_10N");
			Log.i("inei", "P6_1_10N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P6_2
			in = context.getResources().openRawResource(R.raw.sqlp6_2);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P6_2");
			Log.i("inei", "P6_2");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P6_2_4N
			in = context.getResources().openRawResource(R.raw.sqlp6_2_4n);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P6_2_4N");
			Log.i("inei", "P6_2_4N");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P7
			in = context.getResources().openRawResource(R.raw.sqlp7);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P7");
			Log.i("inei", "P7");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P8
			in = context.getResources().openRawResource(R.raw.sqlp8);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P8");
			Log.i("inei", "P8");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// P9
			in = context.getResources().openRawResource(R.raw.sqlp9_f);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("P9_F");
			Log.i("inei", "P9_F");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}
		} catch (Exception e) {

		}

		try {
			DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			// Claves
			in = context.getResources().openRawResource(R.raw.claves);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("claves");
			Log.i("inei", "claves");
			Log.d("inei", "Claves" + statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// Claves
			in = context.getResources().openRawResource(
					R.raw.personal_patrimonio);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("personal");
			Log.i("inei", "personal");
			Log.d("inei", "Personal" + statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			Calendar ca = Calendar.getInstance();
			SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
			String fecha = df.format(ca.getTime());
			SimpleDateFormat dh = new SimpleDateFormat("HH:mm:ss");
			String hora = dh.format(ca.getTime());

			Sesion sesion = new Sesion(context);
			ContentValues c = new ContentValues();
			c.put("dni", sesion.getDNI());
			c.put("cod_pat", sesion.getCP());
			c.put("fecha_reg", fecha);
			c.put("hora_reg", hora);

			Cursor cursor = db
					.rawQuery(
							"select * from personal_patrimonio where dni=? and cod_pat=?",
							new String[] { sesion.getDNI(), sesion.getCP() + "" });
			if (!cursor.moveToFirst()) {
				db.insert("personal_patrimonio", null, c);
				Log.i("DNI", sesion.getDNI());
				Log.i("CP", sesion.getCP());
			}
			cursor.close();

			String ALTER_FRENTE_LINDEROS = "ALTER TABLE FRENTE_LINDEROS ADD COLUMN P4_1_Obs TEXT null";

			String ALTER_P9_F = "ALTER TABLE P9_F ADD COLUMN P9_F_Nro int null";
			Log.i("TABLA 4", "Listo para modificar");
			db.execSQL(ALTER_FRENTE_LINDEROS);
			Log.i("TABLA 4", "Se modifico");
			Log.i("TABLA 9", "Listo para modificar");
			db.execSQL(ALTER_P9_F);
			Log.i("TABLA 9", "Se modifico");

			// Mod
			in = context.getResources().openRawResource(R.raw.mod);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Mod");
			Log.i("inei", "Mod");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// PMod
			in = context.getResources().openRawResource(R.raw.sqlpmod);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("PMod");
			Log.d("inei", statements.getLength() + "");
			Log.i("inei", "PMod");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			// Ugel
			in = context.getResources().openRawResource(R.raw.ugel);
			doc = dbuilder.parse(in, null);
			statements = doc.getElementsByTagName("Ugel");
			Log.i("inei", "Ugel");
			Log.d("inei", statements.getLength() + "");
			for (i = 0; i < statements.getLength(); i++) {
				sql = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(sql);
			}

			String ALTER_P5 = "ALTER TABLE P5 ADD COLUMN P5_Obs text null";
			db.execSQL(ALTER_P5);
			String ALTER_P8 = "ALTER TABLE P8 ADD COLUMN P8_Obs text null";
			db.execSQL(ALTER_P8);
			String ALTER_P1 = "ALTER TABLE P1_A ADD COLUMN P1_Obs text null";
			db.execSQL(ALTER_P1);
			String ALTER_P7 = "ALTER TABLE P7 ALTER COLUMN P7_2_2 text null";
			db.execSQL(ALTER_P7);

		} catch (Exception e) {

		}

	}

	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		// super.onDowngrade(db, oldVersion, newVersion);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		if (oldVersion != newVersion) {
			String sql;
			InputStream in;
			Document doc;
			NodeList statements;
			int i;

			try {
				DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();

				// Claves
				in = context.getResources().openRawResource(R.raw.claves);
				doc = dbuilder.parse(in, null);
				statements = doc.getElementsByTagName("claves");
				Log.d("inei", "Claves" + statements.getLength() + "");
				for (i = 0; i < statements.getLength(); i++) {
					sql = statements.item(i).getChildNodes().item(0)
							.getNodeValue();
					db.execSQL(sql);
				}

				// Claves
				in = context.getResources().openRawResource(
						R.raw.personal_patrimonio);
				doc = dbuilder.parse(in, null);
				statements = doc.getElementsByTagName("personal");
				Log.d("inei", "Personal" + statements.getLength() + "");
				for (i = 0; i < statements.getLength(); i++) {
					sql = statements.item(i).getChildNodes().item(0)
							.getNodeValue();
					db.execSQL(sql);
				}

				Calendar ca = Calendar.getInstance();
				SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
				String fecha = df.format(ca.getTime());
				SimpleDateFormat dh = new SimpleDateFormat("HH:mm:ss");
				String hora = dh.format(ca.getTime());

				Sesion sesion = new Sesion(context);
				ContentValues c = new ContentValues();
				c.put("dni", sesion.getDNI());
				c.put("cod_pat", sesion.getCP());
				c.put("fecha_reg", fecha);
				c.put("hora_reg", hora);

				Cursor cursor = db
						.rawQuery(
								"select * from personal_patrimonio where dni=? and cod_pat=?",
								new String[] { sesion.getDNI(),
										sesion.getCP() + "" });
				if (!cursor.moveToFirst()) {
					db.insert("personal_patrimonio", null, c);
					Log.i("DNI", sesion.getDNI());
					Log.i("CP", sesion.getCP());
				}
				cursor.close();
			} catch (Exception e) {

			}
			try {
				String ALTER_TBL = "ALTER TABLE FRENTE_LINDEROS ADD COLUMN P4_1_Obs TEXT null";
				Log.i("TABLA 4", "Listo para crear");
				db.execSQL(ALTER_TBL);
				Log.i("TABLA 4", "Se creo");
			} catch (Exception e) {

			}

			try {
				String ALTER_P9_F = "ALTER TABLE P9_F ADD COLUMN P9_F_Nro int null";
				Log.i("TABLA 9", "Listo para modificar");
				db.execSQL(ALTER_P9_F);
				Log.i("TABLA 9", "Se modifico");
			} catch (Exception e) {

			}

			try {
				String ALTER_P5 = "ALTER TABLE P5 ADD COLUMN P5_Obs text null";
				db.execSQL(ALTER_P5);
			} catch (Exception e) {

			}
			try {
				String ALTER_P8 = "ALTER TABLE P8 ADD COLUMN P8_Obs text null";
				db.execSQL(ALTER_P8);
			} catch (Exception e) {

			}

			try {
				String ALTER_P1 = "ALTER TABLE P1_A ADD COLUMN P1_Obs text null";
				db.execSQL(ALTER_P1);
			} catch (Exception e) {

			}

			try {
				String ALTER_P7 = "ALTER TABLE P7 ALTER COLUMN P7_2_2 text null";
				db.execSQL(ALTER_P7);
			} catch (Exception e) {

			}

		}
	}
}
